package roborally.script.singleCommand;

import roborally.model.Element;
import roborally.model.Item;
import roborally.model.Robot;
import roborally.script.SingleCommand;
import be.kuleuven.cs.som.annotate.*;
import be.kuleuven.cs.som.taglet.*;

/**
 * @author Joren VERSPEURT
 * @author Mathieu REYMOND
 * @version 3.0
 * 
 * The class representing a Pickup-and-use command. When stepped it will make it's robot pick up and use an item at its position, if there are any.
 */
public class PickupAndUse extends SingleCommand {
	
	/**
	 * Create a new Pickup-and-use command with a given Robot.
	 * @param robot The Robot this Pickup-and-use command will apply to.
	 */
	public PickupAndUse(Robot robot) {
		super(robot);
	}
	
	/**
	 * If there is an item to be picked up at <code>robot</code>'s position, pick it up and use it.
	 */
	public void step() {
		super.step();
		
		for(Element element : robot.getBoard().getElements(robot.getPosition())) {
			if(element.getClass().isAssignableFrom(Item.class)) {
				Item item = (Item) element;
				robot.pickUp(item);
				robot.use(item);
				break;
			}
		}
	}
	
	/**
	 * The script word for this command.
	 */
	@Override
	public String toString() {
		return "pickup-and-use";
	}
}
